<?php
/**
 * Created by PhpStorm.
 * User: chuanbin
 * Date: 2018/12/26
 * Time: 15:44
 */

namespace Admin\Controller;



class AssetController extends AdminController
{
    /**
     * 首页
     */
    public function index(){
        $map = array();//TODO 筛选
        $list   = $this->lists('mobile', $map);
        $count = M('mobile')->where($map)->count();
        $this->assign('_list', $list);
        $this->assign('count', $count);
        $this->meta_title = '签到记录';
        $this->display();
    }

    /**
     * 新增编辑资产
     */
    public function add(){
        if(IS_POST){
            $id = I('id',0,'intval');
            $do_act = I('do_act',0,'intval');
            $obj = M('mobile');
            if($id>0){
                $obj->create();
                $rzt = $obj->where('id='.$id)->save();
                if($do_act>0){
                    //添加操作日志
                    $act_str = array('1'=>'借出',2=>'归还');
                    $info = $obj->find($id);
                    $in['sn'] = $info['zcbh'];
                    $in['mobile_id'] = $id;
                    if($do_act==1){
                        $in['user_name'] = I('now_user');
                        $in['user_part'] = I('now_part');
                    } else {
                        $in['user_name'] = $info['now_user'];
                        $in['user_part'] = $info['now_part'];
                    }

                    $in['action_user'] = UID;
                    $in['create_time'] = time();
                    $in['action_type'] = $act_str[$do_act];
                    $log = M('mobile_log');
                    $log->create($in);
                    $log->add();
                }

            } else {
                $obj->create();
                $rzt = $obj->add();
            }
            if($rzt){
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $map['id'] = I('id',0,'intval');
            $info = M('mobile')->where($map)->find();
            $this->assign('info',$info);
            $this->display('add');
        }
    }

    public function act(){
        if(!$_POST){
            $id = I('id',0,'intval');
            $obj = M('mobile');
            $info = $obj->find($id);
            $this->assign('info',$info);
            $this->display('act');
        } else {
            echo 'post';
        }
    }

    public function back(){
        if(!$_POST){
            $id = I('id',0,'intval');
            $obj = M('mobile');
            $info = $obj->find($id);
            $this->assign('info',$info);
            $this->display('back');
        } else {
            echo 'post';
        }
    }

    /**
     * 导入
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     */
    public function import(){
        if(!empty($_POST)){
            if (!empty($_FILES)){

                /* 调用文件上传组件上传文件 */
                $File = D('File');
                $setting = C('ASSET_XLS');
                $info = $File->upload($_FILES, $setting);

                /* 记录附件信息 */
                if(!$info){
                    $this->error($File->getError());
                }

                vendor("PHPExcel.PHPExcel");
                $extension = $info['file']['ext'];
                $file = $setting['rootPath'].$info['file']['savepath'].$info['file']['savename'];

                if ($extension =='xlsx') {
                    $objReader = new \PHPExcel_Reader_Excel2007();
                    $objExcel = $objReader ->load($file);
                } else if ($extension =='xls') {
                    $objReader = new \PHPExcel_Reader_Excel5();
                    $objExcel = $objReader ->load($file);
                } else if ($extension=='csv') {
                    $PHPReader = new \PHPExcel_Reader_CSV();
                    //默认输入字符集
                    $PHPReader->setInputEncoding('GBK');
                    //默认的分隔符
                    $PHPReader->setDelimiter(',');
                    //载入文件
                    $objExcel = $PHPReader->load($file);
                }

                $sheet = $objExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow(); // 取得总行数
                $highestColumn = $sheet->getHighestColumn(); // 取得总列数
                $r = $objExcel->getActiveSheet();
                $count = 0;
                $obj = M('mobile');

                $rand_key = time().rand(1000,9999);

                for($i=2;$i<=$highestRow;$i++){
//                    $rowData = $sheet->rangeToArray('A'. $i .':'. $highestColumn . $i, NULL, TRUE, FALSE);
                    $data[$i]['zcbh']        = $objExcel->getActiveSheet()->getCell("B".$i)->getValue();
                    $data[$i]['zc_type']         = $objExcel->getActiveSheet()->getCell("C".$i)->getValue();
                    $data[$i]['name']         = $objExcel->getActiveSheet()->getCell("D".$i)->getValue();
                    $data[$i]['model']         = $objExcel->getActiveSheet()->getCell("E".$i)->getValue();
                    $data[$i]['screen_size']         = $objExcel->getActiveSheet()->getCell("F".$i)->getValue();
                    $data[$i]['screen_rate']         = $objExcel->getActiveSheet()->getCell("G".$i)->getValue();
                    $data[$i]['system']         = $objExcel->getActiveSheet()->getCell("H".$i)->getValue();
                    $data[$i]['ram']         = $objExcel->getActiveSheet()->getCell("I".$i)->getValue();
                    $data[$i]['cpu_core']         = $objExcel->getActiveSheet()->getCell("J".$i)->getValue();
                    $data[$i]['cpu_name']         = $objExcel->getActiveSheet()->getCell("K".$i)->getValue();
                    $data[$i]['cpu_rate']         = $objExcel->getActiveSheet()->getCell("L".$i)->getValue();
                    $data[$i]['cpu_model']         = $objExcel->getActiveSheet()->getCell("M".$i)->getValue();
                    $data[$i]['market_time']         = $objExcel->getActiveSheet()->getCell("N".$i)->getValue();
                    $data[$i]['mac']         = $objExcel->getActiveSheet()->getCell("O".$i)->getValue();
                    $data[$i]['now_status']         = $objExcel->getActiveSheet()->getCell("P".$i)->getValue();
                    $data[$i]['part_1']         = $objExcel->getActiveSheet()->getCell("Q".$i)->getValue();
                    $data[$i]['part_2']         = $objExcel->getActiveSheet()->getCell("R".$i)->getValue();
                    $data[$i]['user_part']         = $objExcel->getActiveSheet()->getCell("S".$i)->getValue();
                    $data[$i]['user_name']         = $objExcel->getActiveSheet()->getCell("T".$i)->getValue();
                    $data[$i]['user_uid']         = 0;
                    $data[$i]['device_status']         = $objExcel->getActiveSheet()->getCell("W".$i)->getValue();
                    $data[$i]['price']         = $objExcel->getActiveSheet()->getCell("U".$i)->getValue();
                    $data[$i]['out_user']         = $objExcel->getActiveSheet()->getCell("X".$i)->getValue();
                    $data[$i]['out_date']         = $objExcel->getActiveSheet()->getCell("Y".$i)->getValue();
                    $data[$i]['in_date']         = $objExcel->getActiveSheet()->getCell("Z".$i)->getValue();
                    $data[$i]['remark']         = $objExcel->getActiveSheet()->getCell("AA".$i)->getValue();
                    $data[$i]['device_type']         = 'ios';
                    $data[$i]['udid']         =       $objExcel->getActiveSheet()->getCell("AB".$i)->getValue();
                    $data[$i]['storage']         =    $objExcel->getActiveSheet()->getCell("AC".$i)->getValue();
                    $data[$i]['xianka']         =     $objExcel->getActiveSheet()->getCell("AD".$i)->getValue();
                    $data[$i]['now_system']         = $objExcel->getActiveSheet()->getCell("AE".$i)->getValue();
                    $data[$i]['rand_key']         = $rand_key;
                    $data[$i]['create_time']         = time();
//                    print_r($data);die;
                    $obj->create($data[$i]);
                    $obj->add();


                }
                $this->success('导入成功！');
            }else{
                $this->error("请选择上传的文件");
            }
        }else{
            if(isset($_GET['id']) && $_GET['id']) $this->assign('id',$_GET['id']);
            $this->display('import');
        }
    }


}